import { spugConfig } from '../config/database.js';

export async function sendSpugNotification(phone, code) {
  try {
    console.log(`准备发送验证码到 ${phone}: ${code}`);

    const body = JSON.stringify({
      name: '验证码通知',
      code: code,
      targets: phone
    });

    const response = await fetch(spugConfig.apiUrl, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${spugConfig.accessToken}`
      },
      body: body
    });

    const responseText = await response.text();
    console.log('Spug响应:', response.status, responseText);

    if (response.ok) {
      console.log(`✅ 短信验证码已发送到 ${phone}`);
      return true;
    } else {
      console.error('❌ Spug推送失败:', response.status, responseText);
      return false;
    }
  } catch (error) {
    console.error('❌ 发送Spug推送失败:', error);
    return false;
  }
}